<script setup>
//导入
import {nextTick, ref} from "vue";
import {register} from "../../api/auth/registerApi.js";
import router  from "../../router/index.js";
import {User,Key} from "@element-plus/icons-vue";
import {ElLoading, ElMessage} from "element-plus";
import {clearObject} from "../../utils/utils.js";
// 对象
const registerInfo = ref({
  username:'',
  password:'',
  confirmPassword:''
});
// 方法
// 前往登录页面
const goLogin = () => {
  router.push("/auth/login")
}
// 注册接口
const handleRegister = async () => {
  let loading = ElLoading.service({
    text: "登陆成功，跳转中...",
    spinner: "",
    background: "rgba(0, 0, 0, 0.4)",
    fullscreen: true,
  })
  if(registerInfo.value.password !== registerInfo.value.confirmPassword){
    ElMessage.error("两次输入的密码不一致");
    await nextTick(() => {
      loading.close();
    })
    return;
  }
  // 发送请求
  await register(registerInfo.value).then(res => {
    ElMessage.success("注册成功");
    localStorage.setItem("token",res.data);
    clearObject(registerInfo.value);
    nextTick(() => {
      loading.close();
    })
    router.push("/home")
  }).catch(err => {
    nextTick(() => {
      loading.close();
    })
  })
}



</script>

<template>
  <div class="center-container">
    <el-card class="center-card"  style="border-radius: 20px">
      <div class="card-content">
        <div class="image-container">
          <img src="../../assets/image/backgroud/微信图片_20240613162653.jpg" alt="注册图片" style="width: 100%; height: 100%;">
        </div>
        <div class="text-container">
          <div style="width: 100%;display: flex;text-align: center;justify-content: center;">
            <h1 style="font-size: 30px;margin-top: 2em">智能教育平台</h1>
          </div>
          <el-form :model="registerInfo" label-width="auto" style="max-width: 600px">
            <div style="margin: 2em 2em 1em;">
              <div class="input-group"><el-input class="input-field" :prefix-icon="User" placeholder="请输入用户名" v-model="registerInfo.username" /></div>
              <div class="input-group"><el-input class="input-field" :prefix-icon="Key" type="password"  placeholder="请输入密码" v-model="registerInfo.password"/></div>
              <div class="input-group"><el-input class="input-field"  :prefix-icon="Key" type="password"  placeholder="请再次输入密码" v-model="registerInfo.confirmPassword"/></div>
            </div>
            <el-button type="primary" style="display: flex; width: 23em;margin-bottom: 1em;margin-left: 2em;margin-right: 2em" @click="handleRegister">注册并登录</el-button>
            <div style="display: flex;justify-content: center">
              <span class="same-font-size" style="color: gray;font-size: 15px">已有账号？</span>
              <el-link class="same-font-size" type="primary" @click="goLogin">立即登陆</el-link>
            </div>
          </el-form>
        </div>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.center-container {
  display: flex;
  justify-content: center;/* 将卡片作为父组件从左侧开始放置 */
  align-items: center;
  height: 80vh; /* 将容器垂直居中 */
  margin: 0 auto; /* 水平居中 */
}
.center-card {
  width: 800px; /* 调整卡片大小 */
  height: 460px;
  border-radius: 80px
}
.card-content {
  display: flex;
}
.image-container {
  flex: 1;
  /* 根据需要设置图像容器的样式 */
}
.text-container {
  flex: 1;
  /* 根据需要设置文本容器的样式 */
}
.input-group {
  display: flex;
  align-items: center;
  margin-bottom: 1.4em;
}
.input-field {
  flex: 1;
  height: 3em;
}
.same-font-size{
  font-size: inherit; /* 继承父元素的字体大小 */
  display: inline-block; /* 使元素在同一行显示 */
  vertical-align: middle; /* 垂直居中对齐 */
}
</style>